package com.xinxing.learning.crypto.controller;

import com.xinxing.learning.crypto.annotation.DecryptionAnnotation;
import com.xinxing.learning.crypto.annotation.EncryptionAnnotation;
import com.xinxing.learning.crypto.entity.Result;
import com.xinxing.learning.crypto.entity.ResultBuilder;
import com.xinxing.learning.crypto.entity.Teacher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class TeacherController implements ResultBuilder {

    /**
     * 直接返回对象，不加密
     *
     * @param teacher Teacher对象
     * @return 不加密的对象
     */
    @PostMapping("/get")
    public ResponseEntity<Result<?>> get(@Validated @RequestBody Teacher teacher) {
        log.info("get() called with parameters => 【teacher = {}】", teacher);
        return success(teacher);
    }

    /**
     * 返回加密后的数据
     *
     * @param teacher Teacher对象
     * @return 返回加密后的数据 ResponseBody<Result>格式
     */
    @PostMapping("/encrypt")
    @EncryptionAnnotation
    public ResponseEntity<Result<?>> encrypt(@Validated @RequestBody Teacher teacher) {
        log.info("encrypt() called with parameters => 【teacher = {}】", teacher);
        return success(teacher);
    }

    /**
     * 请求参数为加密
     * 返回加密后的数据
     *
     * @param teacher Teacher对象
     * @return 返回加密后的数据 Result格式
     */
    @PostMapping("/encrypt1")
    @EncryptionAnnotation
    public Result<?> encrypt1(@Validated @RequestBody Teacher teacher) {
        log.info("encrypt1() called with parameters => 【teacher = {}】", teacher);
        return success(teacher).getBody();
    }

    /**
     * 请求参数是加密的
     * 返回解密后的数据
     *
     * @param teacher Teacher对象
     * @return 返回解密后的数据
     */
    @PostMapping("/decrypt")
    @DecryptionAnnotation
    public ResponseEntity<Result<?>> decrypt(@Validated @RequestBody Teacher teacher) {
        log.info("decrypt() called with parameters => 【teacher = {}】", teacher);
        return success(teacher);
    }

    /**
     * 请求参数是加密的要实现解密gongn
     * 响应结果进行加密
     *
     * @param teacher Teacher对象
     * @return 返回解密后的数据
     */
    @PostMapping("/decrypt1")
    @EncryptionAnnotation
    @DecryptionAnnotation
    public ResponseEntity<Result<?>> decrypt1(@Validated @RequestBody Teacher teacher) {
        log.info("decrypt() called with parameters => 【teacher = {}】", teacher);
        return success(teacher);
    }
}
